class LinkNode{
    constructor(value,next) {
        this.value = value;
        this.next = next;
    }
}

const linkedList = new LinkNode(1,null);
linkedList.next = new LinkNode(2, null);
linkedList.next.next = new LinkNode(3, null);
linkedList.next.next.next = new LinkNode(4, null);

const linkedListTraverse = (linkedList) => {
    let pre = null, cur = linkedList;
    while (cur){
        let next = cur.next;
        cur.next = pre;
        pre = cur;
        cur = next;
    }
    return pre;
}

const reserveByKGroup = (head, k) => {
    let pre = null, cur = head, p = head;
    for(let i=0; i<k;i++){
        if(p == null) return head;
        p = p.next;
    }
    for(let i=0; i<k;i++){
        let next = cur.next;
        cur.next = pre;
        pre = cur;
        cur = next;
    }
    head.next = reserveByKGroup(cur,k);
    return pre;
};